package chaw.android.train.MoreActivities;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import chaw.android.train.MoreActivity;
import chaw.android.train.R;
import chaw.android.train.TrainTravelActivity;
import chaw.android.util.HttpRequestManager;
import chaw.android.util.Preferences;
import chaw.android.util.ServerURL;
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class MoreVersionActivity extends ListActivity {
	ListAdapter adapter;
	List<HashMap<String, String>> list;
	private ImageView btnBack;
	private Button moreVersionUpdate;
	private TextView textVersionInfo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.more_version);
		
		btnBack = (ImageView) findViewById(R.id.btnBack);
		moreVersionUpdate = (Button) findViewById(R.id.moreVersionUpdate);
		textVersionInfo = (TextView) findViewById(R.id.textVersionInfo);
		
		btnBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				MoreActivity parentActivity = (MoreActivity) getParent();
				parentActivity.finishFromChild(MoreVersionActivity.this);
			}
		});
		
		moreVersionUpdate.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent homeSite = new Intent(Intent.ACTION_VIEW, Uri.parse(ServerURL.homePage));
				startActivity(homeSite);
			}
		});
	}
	
	private void setListView(){
		DownloadAsynkTask task = new DownloadAsynkTask();
		String[] params = new String[]{ ServerURL.versionInfo };
		task.execute(params);
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		setListView();
		for(int i = 0 ; i < 5; i++)
        	TrainTravelActivity.tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.bg_nav1);
	}
	
	private class DownloadAsynkTask extends AsyncTask<String, Void, String> {
		@Override
		protected String doInBackground(String... urls) {
			// TODO Auto-generated method stub
			String response = null;
			
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			
			if(urls.length > 1)
				for(int i = 1; i< urls.length; i+=2)
					params.add(new BasicNameValuePair(urls[i], urls[i+1]));
			response = HttpRequestManager.request(urls[0], params);
			return response;
		}
		
		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			list = new ArrayList<HashMap<String, String>>();
			String[] from = new String[]{"text","version"};
			int[] to = new int[]{R.id.moreVersionText, R.id.moreVersionInfo};
			
			HashMap<String, String> eachMapCurrent = new HashMap<String, String>();
			eachMapCurrent.put("text", "현재버전 : ");
			eachMapCurrent.put("version", Preferences.getValue(getBaseContext(), "version"));
			list.add(eachMapCurrent);
			
			HashMap<String, String> eachMapLatest = new HashMap<String, String>();
			eachMapLatest.put("text", "최신버전 : ");
			eachMapLatest.put("version", Preferences.getValue(getBaseContext(), "version"));
			list.add(eachMapLatest);
			
			adapter = new SimpleAdapter(getBaseContext(), list, R.layout.more_version_row, from, to);
			setListAdapter(adapter);
			getListView().setBackgroundResource(R.drawable.round_rect_more);
			
			if(result.equalsIgnoreCase(Preferences.getValue(getBaseContext(), "version"))){
				moreVersionUpdate.setVisibility(View.INVISIBLE);
				textVersionInfo.setVisibility(View.INVISIBLE);
			}
			else{
				moreVersionUpdate.setVisibility(View.VISIBLE);
				textVersionInfo.setVisibility(View.VISIBLE);
			}
//			((SimpleAdapter) adapter).notifyDataSetChanged();
		}
	}
}
